#include "StdAfx.h"
#include "DrawCheckBox.h"
namespace UILib
{

    CDrawCheckBox::CDrawCheckBox(void) : CDrawView( NULL, -1, NULL )\
        , pBitMap_(NULL)\
        , nStatusCount_(4)\
        , nGroupCount_(2)\
        , bSel_(false)
        , nOffWidth_(25)
    {
    }

    CDrawCheckBox::CDrawCheckBox( Bitmap* pBitmap, const PUIRect pRect, const UINT uID\
        , const int nStatusCount /*= 4*/, const int nGroupCount /*= 2*/, ViewListener* pListener /*= NULL */ )\
            : CDrawView( pRect, uID, pListener )\
            , pBitMap_(pBitmap)\
            , nStatusCount_(nStatusCount)\
            , nGroupCount_(nGroupCount)\
            , bSel_(false)\
            , nOffWidth_(25)


    {

    }
    CDrawCheckBox::~CDrawCheckBox(void)
    {
    }

    void CDrawCheckBox::DrawElement( const HDC hDC, const RECT &rcPaint )
    {
        if ( pBitMap_ == NULL ||  nStatusCount_ == 0 || nGroupCount_ == 0 || !IsVisible() )
        {
            return;
        }

        int nW = pBitMap_->GetWidth();
        int nH = pBitMap_->GetHeight();

        int nSingleW = nW/( nStatusCount_*nGroupCount_ );

        RECT rcEmem = { 0 };
        CDrawView::GetRECT(rcEmem);
        SIZE szElem = { rcEmem.right - rcEmem.left, rcEmem.bottom - rcEmem.top };
        Elem_Status eStatus = CDrawView::GetStatus();
        int nCurIndex = (int)eStatus;
        if ( !IsEnable() )
        {
            nCurIndex = (int)Elem_Disable;
        }
        if (bSel_)
        {
            nCurIndex += nStatusCount_;
        }
        Graphics graph(hDC);
        Rect rcDest(rcEmem.left, rcEmem.top + (szElem.cy - nH)/2, nSingleW, nH );
        graph.DrawImage(pBitMap_, rcDest, nSingleW*nCurIndex, 0,nSingleW, nH, UnitPixel, NULL, NULL, NULL);

        if (!wszText_.empty())
        {
            RECT rcEdit = {0};
            memcpy(&rcEdit, &rcEmem, sizeof(RECT));
            rcEdit.left += nOffWidth_;
            InflateRect(&rcEdit, -3, -3 );
            HFONT hfont = ::CreateFontIndirect(&lf_);
            HFONT holdFont = (HFONT)::SelectObject(hDC, hfont);
            COLORREF oldColor = ::SetTextColor(hDC, color_ );
            int oldbkgMode = ::SetBkMode(hDC, TRANSPARENT);
            ::DrawText(hDC, wszText_.c_str(), -1, &rcEdit, DT_SINGLELINE | DT_VCENTER |DT_NOPREFIX | DT_EDITCONTROL|DT_LEFT );
            ::SetBkMode(hDC, oldbkgMode);
            ::SelectObject(hDC, holdFont);
            ::SetTextColor(hDC, oldColor);
            ::DeleteObject(hfont);
        }
    }

    void CDrawCheckBox::SetText( wstring wszText, const LOGFONT lf, const COLORREF color /*= 0x000000 */ )
    {
        wszText_ = wszText;
        memcpy(&lf_, &lf, sizeof(LOGFONT));
        color_ = color;
        CDrawView::ForceInvalidate();
    }

    void CDrawCheckBox::SetText( wstring wszText )
    {
        wszText_ = wszText;
        CDrawView::ForceInvalidate();
    }
    void CDrawCheckBox::SetSel( const bool bSel )
    {
        bSel_ = bSel;
        CDrawView::ForceInvalidate();
    }

    void CDrawCheckBox::HandleMessage( UINT uMsg, WPARAM wParam, LPARAM lParam, HWND hWnd, BOOL& bHandled )
    {
        CDrawView::bNeedInvalidate_ = false;
        if ( !IsVisible() || !IsEnable() )
        {
            return;
        }

        RECT rcElmem = {0};
        CDrawView::GetRECT(rcElmem);
        bool bInElem =  HitCursor( lParam, rcElmem );
        switch( uMsg )
        {
        case WM_MOUSEMOVE :
            {
                if (bInElem)
                {
                    if ( eStatus_ != Elem_Hover && !bLBDown_ )
                    {
                        eStatus_ = Elem_Hover;
                        bNeedInvalidate_ = true;
                    }
                }
                else if ( !bLBDown_ )
                {
                    if (  eStatus_ != Elem_Normal )
                    {
                        eStatus_ = Elem_Normal;
                        bNeedInvalidate_ = true;
                    }
                }
            }	
            break;
        case WM_MOUSELEAVE :
            {
                if (  eStatus_ != Elem_Normal && !bLBDown_ )
                {
                    eStatus_ = Elem_Normal;
                    bNeedInvalidate_ = true;
                }
            }	
            break;
        case WM_LBUTTONDOWN :
            {
                if (  bInElem  )
                {
                    if ( eStatus_ != Elem_Down )
                    {
                        eStatus_ = Elem_Down;
                        bNeedInvalidate_ = true;
                    }
                    bLBDown_ = true;
                }
            }
            break;
        case WM_LBUTTONUP :
            {
                if (bLBDown_)
                {
                    bLBDown_ = false;
                    bNeedInvalidate_ = true;
                    if (  !bInElem  )
                    {
                        eStatus_ = Elem_Normal;
                    }
                    else
                    {
                        eStatus_ = Elem_Hover;
                    }
                }

                if (bInElem)
                {
                    eStatus_ = Elem_Hover;
                    bNeedInvalidate_ = true;

                    SetSel(!bSel_);
                    if ( pListener_ )
                    {
                        dynamic_cast<CheckBoxListener*>(pListener_)->OnCheckChanged( bSel_ , CDrawView::GetID() );
                    }
                }
                else
                {
                    eStatus_ = Elem_Normal;
                }
            }
            break;
        }
    }

    void CDrawCheckBox::SetTextOff( const int nOffW )
    {
        nOffWidth_ = nOffW;
    }

}